{% load codalab_tags %}

{% if phase == None %}

{% elif error %}
    <p><strong>There is a problem with the leaderboard</strong></p>
    <p>Are all of the leaderboard column names matched with scores saved in scores.txt?</p>
    <pre>{{ error }}</pre>

{% elif phase.is_future and  block_leaderboard_view %}
    <p>This phase has not started yet. The leaderboard is still hidden to participants but visible to organizers</p>
{% elif phase.is_blind and not is_owner and not request.user in competition_admins %}
    <p>Competition organizers have chosen to keep the leaderboard private. Please check back later.</p>
{% else %}
    {% if phase.is_blind and is_owner or phase.is_blind and request.user in competition_admins %}
        <p>Competition organizers have chosen to keep the leaderboard private but as an organizer, you can access this page.</p>
    {% endif %}
    <div class="phase-description">
        <h4>Phase description</h4>
        {% if phase.description %}
            <p>{{ phase.description }}</p>
        {% else %}
            <p>None</p>
        {% endif %}
        <p><b>Max submissions per day: </b> {{ phase.max_submissions_per_day }}</p>
        <p><b>Max submissions total: </b> {{ phase.max_submissions }}</p>
    </div>

    {% if groups|length > 0 %}
        <a class="icon-excel btn btn-default" href="/competitions/{{phase.competition.id}}/results/{{phase.id}}/data">Download CSV</a>
    {% endif %}
    {% if request.user == phase.competition.creator or request.user in phase.competition.admins.all %}
        <a class="icon-download btn btn-default" href="{% url "competitions:download_leaderboard_results" competition_pk=phase.competition.pk phase_pk=phase.pk %}">Download all submissions on leaderboard</a>
    {% endif %}

    <div class="leaderboard-result-table">
        {% for group in groups %}
            <table class="resultsTable dataTable">
                <tr class="table-title">
                    <th colspan="{{group.total_span|add:phase.competition.enable_medical_image_viewer|add:phase.competition.allow_teams|add:phase.competition.enable_detailed_results}}">{{group.label}}</th>
                </tr>
                <tr class="leaderboard-result-table-header">
                    <th rowspan="2">#</th>
                    <th rowspan="2">User</th>
                    <th rowspan="2">Entries</th>
                    <th rowspan="2">Date of Last Entry</th>
                    {% if phase.competition.allow_teams %}
                    <th rowspan="2">Team Name</th>
                    {% endif %}
                    {% for head in group.headers %}
                        {% if head.subs %}
                            <th colspan="{{head.subs|length}}">{{head.label}}</th>
                        {% else %}
                            <th rowspan="2" {% if group.selection_key == head.key %} class="column-selectable column-selected" {% else %} class="column-selectable" {% endif %} name="{{head.key}}">{{head.label}}
                            <span class="glyphicon glyphicon-play ascending-sorting" aria-hidden="true"></span>
                            </th>
                        {% endif %}
                    {% endfor %}
                    {% if phase.competition.enable_detailed_results %}
                        <th style="padding: 4px 0;">Detailed Results</th>
                    {% endif %}
                    {% if phase.competition.enable_medical_image_viewer %}
                        <th>Image Viewer</th>
                    {% endif %}
                </tr>
                <tr>
                    {% for head in group.headers %}
                        {% if head.subs %}
                            {% for sub in head.subs %}
                                <th {% if group.selection_key == sub.key %} class="column-selectable column-selected" {% else %} class="column-selectable" {% endif %} name="{{sub.key}}">{{sub.label}}
                                    <span class="glyphicon glyphicon-play ascending-sorting" aria-hidden="true"></span>
                                </th>
                            {% endfor %}
                        {% endif %}
                    {% endfor %}
                </tr>
                {% if group.scores.items|length_is:"0" %}
                    <tr class="noData">
                        <td colspan="{{group.total_span|add:phase.competition.enable_medical_image_viewer|add:phase.competition.enable_detailed_results}}">No data available in table</td>
                    </tr>
                {% else %}
                    {% for pk, scoredata in group.scores %}
                        <tr {% if phase.competition.anonymous_leaderboard and request.user.username == scoredata.username %}style="background-color: #D7FFFA;"{% endif %}>
                            <td class="row-position">
                                {% if forloop.counter < 4 %}
                                    <p class="leader-top3">{{ forloop.counter }}</p>
                                {% else %}
                                    {{ forloop.counter }}
                                {% endif %}
                            </td>
                            <td>
                                {% if phase.competition.anonymous_leaderboard %}
                                    {% if forloop.counter < 4 %}
                                        <p class="leader-top3"><i>Anonymous</i></p><
                                    {% else %}
                                        <i>Anonymous</i>
                                    {% endif %}
                                {% else %}
                                    {% if forloop.counter < 4 %}
                                        <p class="leader-top3">{{ scoredata.username }}</p>
                                    {% else %}
                                        {{ scoredata.username }}
                                    {% endif %}
                                {% endif %}
                            </td>
                            <td> {{ scoredata.count }} </td>
                            <td> {{ scoredata.date|date:"m/d/y" }} </td>
                            {% if phase.competition.allow_teams %}
                            <td>{{ scoredata.team_name|default_if_none:"" }}</td>
                            {% endif %}
                            {% for head in group.headers %}
                                {% for head_or_sub in head|get_array_or_attr:'subs' %}
                                    {% with scoredata.values|get_by_name:head_or_sub.key|first as v %}
                                        {% if 'rnk' in v %}
                                            <td {% if group.selection_key == v.name %} class="column-selected" {% endif %} name={{v.name}}>{{v.val}} (<span class="rank">{{v.rnk}}</span>)</td>
                                        {% else %}
                                            <td {% if group.selection_key == v.name %} class="column-selected" {% endif %} name={{v.name}}>{{v.val}} <span class="rank hide">{{v.hidden_rnk}}</span></td>
                                        {% endif %}
                                    {% endwith %}
                                {% endfor %}
                            {% endfor %}
                            {% if phase.competition.enable_detailed_results %}
                                <td>
                                    <a href="/my/competition/submission/{{scoredata.id}}/detailed_results" target="_blank">
                                        View
                                    </a></br>
                                </td>
                            {% endif %}
{#                            {% if phase.competition.enable_medical_image_viewer %}#}
{#                                {% with "http://cirrusweb.cloudapp.net:8080/Packages/MeVisLab/Private/Sources/Web/application/Presentation.html?module=CodaLabViewer&useTiling=1" as viewerlink %}#}
{#                                    <td>#}
{#                                        <a href="{{viewerlink}}&imageData={{phase.input_data.name|get_sas}}&overlayData={{scoredata.resultLocation|get_sas}}" onclick="window.open('{{viewerlink}}&imageData={{phase.input_data.name|get_sas}}&overlayData={{scoredata.resultLocation|get_sas}}', '_blank', 'width=1000, height=1000, location=0'); return false;">#}
{#                                            View results#}
{#                                        </a>#}
{#                                    </td>#}
{#                                {% endwith %}#}
{#                            {% endif %}#}
                        </tr>
                    {% endfor %}
                {% endif %}
            </table>
        {% endfor %}
    </div>
{% endif %}
